using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.ETL
{
    /// <summary></summary>
    [Serializable]
    public class BFileDownloadInstruction:InstructionBase
    {
        #region props
        private DataProviderType _DbProvider;
        /// <summary></summary>
        public DataProviderType DbProvider
        {
            get { return _DbProvider; }
            set { _DbProvider = value; }
        }

        private string _ConnStr;
        /// <summary></summary>
        public string ConnStr
        {
            get { return _ConnStr; }
            set { _ConnStr = value; }
        }

        private string _SelectSql;
        /// <summary></summary>
        public string SelectStatement
        {
            get { return _SelectSql; }
            set { _SelectSql = value; }
        }

        private string _FileNameField;
        /// <summary></summary>
        public string FileNameField
        {
            get { return _FileNameField; }
            set { _FileNameField = value; }
        }

        private string _BFileField;
        /// <summary></summary>
        public string BFileField
        {
            get { return _BFileField; }
            set { _BFileField = value; }
        }

        private string _DownloadToFolderPath;
        /// <summary></summary>
        public string DownloadToFolder
        {
            get { return _DownloadToFolderPath; }
            set { _DownloadToFolderPath = value; }
        }

        private string _FileNameFormat;
        /// <summary></summary>
        public string FileNameFormat
        {
            get
            {
                return this._FileNameFormat;
            }
            set
            {
                this._FileNameFormat = value;
            }
        }

        private string _LogFilePath;
        /// <summary></summary>
        public string LogFilePath
        {
            get
            {
                return this._LogFilePath;
            }
            set
            {
                this._LogFilePath = value;
            }
        }
        private string _ErrorFilePath;
        /// <summary></summary>
        public string ErrorFilePath
        {
            get
            {
                return this._ErrorFilePath;
            }
            set
            {
                this._ErrorFilePath = value;
            }
        }
        #endregion
        /// <summary></summary>
        public BFileDownloadInstruction():base(Guid.Empty, Guid.Empty)
        {
            this._DbProvider = DataProviderType.Oracle;
            this._ConnStr = string.Empty;
            this._BFileField = string.Empty;
            this._FileNameField = string.Empty;
            this._DownloadToFolderPath = string.Empty;
            this._SelectSql = string.Empty;
            this._FileNameFormat = "{0}";
            this._LogFilePath = string.Empty;
            this._ErrorFilePath = string.Empty;
        }
        /// <summary></summary>
        public BFileDownloadInstruction(Guid componentID, Guid jobTicketID, 
            DataProviderType dbProvider, string connStr, 
            string bfileField, string fileNameField, string selectSql, 
            string tgtFolderPath, string fileNameFormat, 
            string logFilePath, string errorFilePath) 
            : base(componentID, jobTicketID)
        {
            this._DbProvider = dbProvider;
            this._ConnStr = connStr;
            this._BFileField = bfileField;
            this._FileNameField = fileNameField;
            this._DownloadToFolderPath = tgtFolderPath;
            this._SelectSql = selectSql;
            this._FileNameFormat = fileNameFormat;
            this._LogFilePath = logFilePath;
            this._ErrorFilePath = errorFilePath;
        }

        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = XmlDataUtil.AddElement(ref xDoc, parentNode, "Instruction");
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Type", this.GetType().FullName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ComponentID", this.OwnerComponentID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "WorkerID", this.OwnerWorkerInstanceID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "SubmitTime", this.SubmitTime.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "StartTime", this.StartTime.ToString());

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "DbProvider", ((int) this.DbProvider).ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ConnStr", this.ConnStr);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "SelectStatement", this.SelectStatement);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "BFileField", this.BFileField);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FileNameField", this.FileNameField);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "DownloadTo", this.DownloadToFolder);
            XmlDataUtil.UpdateAttribute(ref xDoc,xNode,"FileNameFormat",this.FileNameFormat);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "LogFilePath",this.LogFilePath);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ErrorFilePath",this.ErrorFilePath);
            
            return xNode;
        }

        public override InstructionBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                BFileDownloadInstruction instruction = new BFileDownloadInstruction();
                instruction.OwnerComponentID = new Guid(xNode.Attributes.GetNamedItem("ComponentID").Value);
                instruction.OwnerWorkerInstanceID = new Guid(xNode.Attributes.GetNamedItem("WorkerID").Value);
                instruction.SubmitTime = DateTime.Parse(xNode.Attributes.GetNamedItem("SubmitTime").Value);
                instruction.StartTime = DateTime.Parse(xNode.Attributes.GetNamedItem("StartTime").Value);

                instruction.DbProvider = (DataProviderType) int.Parse(xNode.Attributes.GetNamedItem("DbProvider").Value);
                instruction.ConnStr = xNode.Attributes.GetNamedItem("ConnStr").Value;
                instruction.SelectStatement = xNode.Attributes.GetNamedItem("SelectStatement").Value;
                instruction.BFileField = xNode.Attributes.GetNamedItem("BFileField").Value;
                instruction.FileNameField = xNode.Attributes.GetNamedItem("FileNameField").Value;
                instruction.DownloadToFolder = xNode.Attributes.GetNamedItem("DownloadTo").Value;
                instruction.FileNameFormat = xNode.Attributes.GetNamedItem("FileNameFormat").Value;
                instruction.LogFilePath = xNode.Attributes.GetNamedItem("LogFilePath").Value;
                instruction.ErrorFilePath = xNode.Attributes.GetNamedItem("ErrorFilePath").Value;

                return instruction;
            }
            else
            {
                throw new Exception("Unable to instantiate instruction: invalid type specified in xml node");
            }
        }
    }
}
